package org.foxas.map
{
	import flash.text.TextFormatAlign;
	
	public class FMapData
	{
		/**
		 * @param _pos
		 * @return
		 */
		protected var grids:Array = new Array();
		public function get girdsData():Array{
			return grids;
		}
		public function get colLength():int{
			if(grids.length==0){
				return 0;
			}else{
				return grids[0].length;
			}
		}
		public function get rowLength():int{
			return grids.length;
		}
		public function initByArray(_arr:Array):void{
			this.clear();
			for(var _row:int=0;_row<_arr.length;_row++){
				grids[_row] = new Array();
				for(var _col:int=0;_col<_arr[_row].length;_col++){
					grids[_row][_col] = _arr[_row][_col];
				}
			}
		}
		public function initByOrderArray(_arr:Array,_rowLength:int):void{
			this.clear();
			for(var _row:int=0;_row<_rowLength;_row++){
				grids[_row] = new Array();
				for(var _col:int=0;_col<Math.ceil(_arr.length/_rowLength);_col++){
					grids[_row][_col] = _arr[_row*_rowLength+_col];
				}
			}
		}
		public function creat(_rowLength:uint,_colLength:uint,_state:int=0):void{
			clear();
			for(var _row:int=0;_row<_rowLength;_row++){
				grids[_row] = new Array();
				for(var _col:int=0;_col<_colLength;_col++){
					grids[_row][_col] = _state;
				}
			}
		}
		public function resize(_rowLength:uint,_colLength:uint,_state:int=0):void{
			var _rowLengthLast:int = this.rowLength;
			var _row:int = 0;var _col:int = 0;
			if(_rowLength<_rowLengthLast){
				for(_row=_rowLength;_row<_rowLengthLast;_row++){
					grids[_row] = [];
				}
			}else{
				for(_row=_rowLengthLast;_row<_rowLength;_row++){
					grids[_row] = new Array();
					for(_col=0;_col<_colLength;_col++){
						grids[_row][_col] = _state;
					}
				}
			}
			//
			var _colLengthLast:int = this.colLength;
			for(_row=0;_row<this.rowLength;_row++){
				if(_colLength<_colLengthLast){
					grids[_row].splice(_colLength,_colLengthLast-_colLength);
				}else{
					for(_col=_colLengthLast;_col<_colLength;_col++){
						grids[_row][_col] = _state;
					}
				}
			}
		}
		public function clear():void{
			for(var _row:int=0;_row<this.rowLength;_row++){
				grids[_row] = [];
			}
			grids = [];
		}
		public function cloneFrom(_data:FMapData):void{
			clear();
			for(var _row:int= 0;_row<_data.rowLength;_row++){
				grids[_row] = new Array();
				for(var _col:int= 0;_col<_data.colLength;_col++){
					this.grids[_row][_col] = _data.getState(_row,_col);;
				}
			}
		}
		public function clone(_data:FMapData):FMapData{
			var _dataNew:FMapData = new FMapData();
			_dataNew.cloneFrom(_data);
			var k:Object
			return _dataNew;
		}
		public function setState(_row:int,_col:int,_state:int=0):void{
			grids[_row][_col] = _state;
		}
		public function getState(_row:int,_col:int):int{
			try{
				if(grids[_row][_col]==undefined || grids[_row][_col]==null){
					return -1;
				}
				return grids[_row][_col];
			}catch(_e:Error){
			}
			return -1;
		}
		/**
		 * @param _rowStart 
		 * @param _colStart
		 * @param _data 
		 */		
		public function setStateByData(_rowStart:int,_colStart:int,_data:FMapData):void{
			for(var _row:int=_rowStart;_row<_data.rowLength;_row++){
				for(var _col:int=_colStart;_col<_data.colLength;_col++){
					this.grids[_row][_col] = _data.getState(_row,_col);
				}
			}
		}
		/**
		 * @param _order
		 * @return 
		 * 
		 */		
		public function getColByOrder(_order:int):int{
			return int(_order%this.rowLength);
		}
		public function getRowByOrder(_order:int):int{
			return int(_order/this.colLength);
		}
		public function getOrderByColRow(_row:int,_col:int):int{
			return _row*this.colLength+_col;
		}
	}
}